package com.yx.changdao.web.controller;

import com.yx.changdao.common.data.Pager;
import com.yx.changdao.common.entity.Department;
import com.yx.changdao.common.utils.Faster;
import com.yx.changdao.service.DepartmentService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yx.changdao.common.annotation.AutoLog;
import com.yx.changdao.common.annotation.ShiroDesc;
import com.yx.changdao.common.data.Result;
import com.yx.changdao.common.exception.AlertException;
import com.yx.changdao.common.utils.SysConst;
import com.yx.changdao.common.utils.SysUtils;
import com.yx.changdao.service.SysUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 部门表
 *
 * @Auther: flexible
 * @create 2020/04/02 14:20
 */
@Validated
@RestController
@RequestMapping("/department")
public class DepartmentController {

    /**
     * 部门表服务
     */
    @Autowired
    private DepartmentService departmentService;

    /**
     * 列表
     *
     * @return Result
     */
    @AutoLog
    @GetMapping("/list")
    @RequiresPermissions("department:list")
    @ShiroDesc("部门:列表")
    public Result list(@RequestParam(value = "page") Long page,
                       @RequestParam(value = "limit", required = false) Integer limit) throws AlertException {
        if (limit == null) {
            limit = SysConst.LIMIT_PAGE;
        }
        // 分页配置
        Page<Department> pager = new Page<>(page, limit);
        // 根据请求解析条件。
        QueryWrapper<Department> qw = SysUtils.autoCnd(Department.class);
        // 获取分页结果集。
        IPage<Department> departmentPage = this.departmentService.page(pager, qw);
        List<Department> departmentList = departmentPage.getRecords();
        Map<String, Object> map = new HashMap<>();

        map.put("list", departmentList);
        map.put("pager", new Pager(departmentPage));
        return Result.ok().data(map);
    }


    /**
     * 添加部门表
     *
     * @param
     * @return Result
     */
    @AutoLog
    @PostMapping
    /*@RequiresPermissions("department:add")
    @ShiroDesc("部门:添加")*/
    @RequiresPermissions("department:list")
    @ShiroDesc("部门:列表")
    public Result add(@Valid Department department) throws AlertException {

        Department name = departmentService.getByDptName(department.getDepName());
        if(!Faster.isNull(name)){
               throw new AlertException("部门名称已存在");
        }

        boolean b = departmentService.save(department);
        if (!b) {
            throw new AlertException("部门表添加失败");
        }

        return Result.ok("部门表添加成功");
    }


    /**
     * 获得指定id 部门表。
     *
     * @param
     * @return Result
     */
    @AutoLog
    @GetMapping("/{depId}")
    /*@RequiresPermissions("department:look")
    @ShiroDesc("部门:查看")*/
    @RequiresPermissions("department:list")
    @ShiroDesc("部门:列表")
    public Result get(@PathVariable("depId") String depId) throws AlertException {
        Department department = departmentService.getById(depId);

        if (department == null) {
            throw new AlertException("未找到相关部门表信息");
        }

        return Result.ok().data(department);
    }


//    /**
//     * 更新部门表
//     *
//     * @param
//     * @return Result
//     */
//    @AutoLog
//    @PutMapping
//    @RequiresPermissions("department:save")
//    @ShiroDesc("部门:保存")
//    public Result update(@Valid Department department,
//                         @NotNull(message = "部门id不能为空") Integer depId) throws AlertException {
//        department.setUpdateByName(SysUtils.currentUser().getNickname());
//        boolean b = departmentService.updateById(department);
//        if (!b) {
//            throw new AlertException("部门表更新失败");
//        }
//
//        return Result.ok("部门表更新成功");
//    }


    /**
     * 删除部门表
     *
     * @param
     * @return Result
     */
    @AutoLog
    @DeleteMapping("/{depId}")
    /*@RequiresPermissions("department:del")
    @ShiroDesc("部门:删除")*/
    @RequiresPermissions("department:list")
    @ShiroDesc("部门:列表")
    public Result delete(@PathVariable("depId") String depId) throws AlertException {

        boolean b = departmentService.removeById(depId);
        if (!b) {
            throw new AlertException("部门表删除失败");
        }
        return Result.ok("删除部门表成功");
    }


    /**
     * 部门下拉列表
     *
     * @param
     * @return Result
     */
    @AutoLog
    @GetMapping("/select")
    public Result select() throws AlertException {

        return Result.ok().data(departmentService.departments());

    }

}
